<?php
/**
 * @file
 *
 */
class string {
	/**
	 * 
	 * 
	 */
	function string() {
		die( "Class string can not instantiated!" );
	}
	/**
	 * 剪切字符串
	 * @param string $str
	 * @param number $start
	 * @param number $limit
	 * @return unknown|Ambigous <unknown, string>
	 */
	function substring( $str, $start = 0, $limit = 12 ) {
		if ( 'gbk' == strtolower( WIKI_CHARSET ) ) {
			$strlen = strlen( $str );
			if ( $start >= $strlen ) {
				return $str;
			}
			$clen = 0;
			for ( $i = 0; $i < $strlen; $i++, $clen++ ) {
				if ( ord( substr( $str, $i, 1 ) ) > 0xa0 ) {
					if ( $clen >= $start ) {
						$tmpstr .= substr( str, $i, 2 );
					}
					$i++;
				} else {
					if ( $clen >= $start ) {
						$tmpstr  .= substr( $str, $i, 1 );
					}
				}
				if ($clen >= $start + $limit ) {
					break;
				}
			}
			$str = $tmpstr;
		} else {
			$patten = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/";
			preg_match_all( $patten, $str, $regs );
			$s = '';
			for ( $i = $start; $i < count( $regs[0] ); $i++ ) {
				$s .= $regs[0][$i];
				if ( $i >= $limit ) {
					break;
				}
			}
			$str = $s;
		}
		return $str;
	}
	
	/**
	 * 转码
	 * @param unknown $str
	 * @param string $to
	 * @param string $from
	 * @param string $force
	 * @return unknown|Ambigous <string, unknown>
	 */
	function hiconv( $str, $to = '', $from = '', $force = false) {
		if ( empty( $str ) ) return $str;
		
		# is contain chinese char
		if (!preg_match( '/[\x80-\xff]/', $str ) ) {
			return $str; 
		}
		if ( empty( $to ) ) {
			if ( 'utf-8' == strtolower( WIKI_CHARSET ) ) {
				return $str;
			}
			$to = WIKI_CHARSET;
		}
		if ( empty( $from ) ) {
			$from = ('gbk' == strtolower( $to ) ) ? 'utf-8' : 'gbk';
		}
		$to = strtolower( $to );
		$from = strtolower( $from );
		//$isutf8=preg_match( '/^([\x00-\x7f]|[\xc0-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xf7][\x80-\xbf]{3})+$/', $str );
		$re = strlen($str) > 6 ? '/([\xe0-\xef][\x80-\xbf]{2}){2}/' : '/[\xe0-\xef][\x80-\xbf]{2}/';
		$isutf8 = preg_match($re, $str);
		
		//$force = (substr($to, 0, 3) == 'utf') ? true : $force;
		
		if ( !$force && $isutf8 && $to == 'utf-8' ) {
			return $str;
		}			
		if( !$force && !$isutf8 && $to == 'gbk' ) {
			return $str;
		}
		
		if ( function_exists( 'iconv' ) ) {
			$str = iconv( $from, $to, $str );
		} else {
			require_once( HDWIKI_ROOT.'/lib/Chinese.class.php' );
			$ch = new chinese( $from, $to );
			if( 'utf-8' == $from ) {
				$str = addslashes( $ch->convert( stripslashes( $str ) ) );
			}else{
				$str = $ch->convert( $str );
			}
		}
		return $str;
	}

	/**
	 * 取得字符串长度
	 * @param string $str
	 * @return number
	 */
	function hstrlen($str) {
		if( 'gbk' == strtolower( WIKI_CHARSET ) ) {
			$length = strlen( $str );
		} else {
			$length = floor( 2/3*strlen( $str ) );
		}
		return $length;
	}
	/**
	 * 转换为大写 
	 * @param unknown $str
	 * @return string
	 */
	function hstrtoupper( $str ) {
		if (is_array($str)){
			foreach ($str as $key => $val){
				$str[$key] = string::hstrtoupper($val);
			}
		}else{
			$i=0;
			$total = strlen($str);
			$restr = '';
			for ($i=0; $i<$total; $i++){
				$str_acsii_num = ord($str[$i]);
				if($str_acsii_num>=97 and $str_acsii_num<=122){
					$restr.=chr($str_acsii_num-32);
				}else{
					$restr.=chr($str_acsii_num);
				}
			}
		}
		return $restr;
	}
	
	/**
	 * 转换为小写 
	 * @param unknown $string
	 * @return string
	 */
	function hstrtolower( $string ) {
		if ( is_array ( $string ) ) {
			foreach ($string as $key => $val){
				$string[$key] = string::hstrtolower( $val );
			}
		} else {
			$string = strtolower( $string );
		}
		return $string;
	}
	
	/**
	 * 转义字符 
	 * @param unknown $string
	 * @param number $force
	 * @return string
	 */
	static function haddslashes( $string, $force = 0 ) {
		if ( !MAGIC_QUOTES_GPC || $force ) {
			if ( is_array( $string ) ) {
				foreach( $string as $key => $val ) {
					$string[$key] = string::haddslashes( $val, $force );
				}
			} else {
				$string = addslashes( $string );
			}
		}
		return $string;
	}
	
	/**
	 * 去除转义
	 * @param unknown $string
	 * @return string
	 */
	function hstripslashes( $string ) {
		if ( is_array( $string ) ) {
			while ( @list( $key,$var ) = @each( $string ) ) {
				if ( $key != 'argc' 
					&& $key != 'argv' 
					&& ( strtoupper($key) != $key || ''.intval( $key ) == "$key")
					) {
					if ( is_string( $var ) ) {
						$string[$key] = stripslashes( $var );
					}
					if ( is_array( $var ) )  {
						$string[$key] = string::hstripslashes($var);
					}
				}
			}
		} else {
			$string = stripslashes( $string );
		}
		return $string;
	}
	
	/**
	 * 替换换行符
	 * @param unknown $str
	 * @return mixed
	 */
	function convercharacter( $str ) {
		$str = str_replace( '\\\r', "", $str );
		$str = str_replace( '\\\n', "", $str );
		$str = str_replace( '\n', "", $str );
		$str = str_replace( '\r', "", $str );
		return $str;
	}

	/**
	 * 获得拼音首字母
	 * @param unknown $string
	 * @return number|string
	 */
	function getfirstletter( $string ) {
		if ( WIKI_CHARSET =='UTF-8' ) {
			$string = string::hiconv( $string, 'gbk', 'utf-8' );
		}
		$dict = array(
			'a'=>0xB0C4,'b'=>0xB2C0,'c'=>0xB4ED,'d'=>0xB6E9,
			'e'=>0xB7A1,'f'=>0xB8C0,'g'=>0xB9FD,'h'=>0xBBF6,
			'j'=>0xBFA5,'k'=>0xC0AB,'l'=>0xC2E7,'m'=>0xC4C2,
			'n'=>0xC5B5,'o'=>0xC5BD,'p'=>0xC6D9,'q'=>0xC8BA,
			'r'=>0xC8F5,'s'=>0xCBF9,'t'=>0xCDD9,'w'=>0xCEF3,
			'x'=>0xD1B8,'y'=>0xD4D0,'z'=>0xD7F9,
			);
		$letter = substr ( $string, 0, 1 );
		$letter_ord = ord ( $letter );
		if ( $letter_ord >= 176 && $letter_ord <= 215 ) {
			$num = '0x' . bin2hex( substr( $string, 0, 2 ) );
			foreach ( $dict as $k => $v ) {
				if ( $v >= $num ) 
					break;
			}
			return $k;
		} elseif ( ( $letter_ord > 64 && $letter_ord < 91 ) || ( $letter_ord > 96 && $letter_ord < 123 ) ) {
			return $letter;
		} elseif ( $letter >= '0' && $letter <= '9' ) {
			return $letter;
		} else {
			return '*';
		}
	}
	
	/**
	 * 过滤特殊字符
	 * @param unknown $string
	 * @return string
	 */
	function stripspecialcharacter ( $string ) {
		$string = trim( $string );
		$string = str_replace( "&", "", $string );
		$string = str_replace( "\'", "", $string );
		$string = str_replace( "'", "", $string );
		$string = str_replace("&amp;amp;","",$string );
		$string = str_replace( "&amp;quot;", "", $string );
		$string = str_replace( "\"", "", $string );
		$string = str_replace( "&amp;lt;", "", $string );
		$string = str_replace( "<", "", $string );
		$string = str_replace( "&amp;gt;", "", $string );
		$string = str_replace( ">", "", $string );
		$string = str_replace( "&amp;nbsp;", "", $string );
		$string = str_replace( "\\\r", "", $string );
		$string = str_replace( "\\\n", "", $string );
		$string = str_replace( "\n", "", $string );
		$string = str_replace( "\r", "", $string );
		$string = str_replace( "\r", "", $string );
		$string = str_replace( "\n", "", $string );
		$string = str_replace( "'", "&#39;", $string );
		$string = nl2br( $string );
		return $string;
	}

	/**
	 * 转为unicode
	 * @param unknown $string
	 * @return Ambigous <string, mixed>
	 */
	function convert_to_unicode( $string ) {
		if ( WIKI_CHARSET == 'GBK' ) {
			$string = string::hiconv( $string, 'utf-8', 'gbk' );
		}
		$string= preg_replace( "/([\\xc0-\\xff][\\x80-\\xbf]*)/e", "' U8'.bin2hex( \"$1\" )", string::hstrtolower( $string ) );
		if ( strlen( $string ) < 4 ) {
			$string = ' HDWIKI' . $string;
		}
		return $string;
	}

	/**
	 * 过滤script代码
	 * @param unknown $string
	 * @return mixed
	 */
	function stripscript( $string ) {
		$pregfind = array( "/<script.*>.*<\/script>/siU", '/on(mousewheel|mouseover|click|load|onload|submit|focus|blur)="[^"]*"/i' );
		$pregreplace = array('', '', );
		$string = preg_replace( $pregfind, $pregreplace, $string );
		return $string;
	}
}
?>